﻿Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Text
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls

Namespace DotNetNuke.Modules.ActiveForums.Controls
    <DefaultProperty("Text"), ToolboxData("<{0}:MenuButton runat=server></{0}:MenuButton>")> _
    Public Class MenuButton
        Inherits WebControl
#Region "Declarations"
        Private _Text As String
        Private _Menu As MenuContent
        Private _MenuCss As String
        Private _MenuWidth As Integer = 100
        Private _MenuHeight As Integer = 100
        Private _MenuOverflow As String = "hidden"
        Private _OffsetLeft As Integer = 0
        Private _OffsetTop As Integer = 16
        Private _AnimationSteps As Integer = 5
        Private _AnimationDelay As Integer = 20
        Enum ExpandDirections
            DownRight
            DownLeft
            UpRight
            UpLeft
        End Enum
        Private _ExpandDirection As ExpandDirections = ExpandDirections.DownRight
#End Region
#Region "Properties"
        Property Text() As String
            Get
                Return _Text
            End Get
            Set(ByVal value As String)
                _Text = value
            End Set
        End Property
        Property Menu() As MenuContent
            Get
                EnsureChildControls()
                If _Menu Is Nothing Then
                    _Menu = New MenuContent
                End If
                Return _Menu
            End Get
            Set(ByVal value As MenuContent)
                _Menu = value
            End Set
        End Property
        Property MenuCss() As String
            Get
                Return _MenuCss
            End Get
            Set(ByVal value As String)
                _MenuCss = value
            End Set
        End Property
        Property MenuWidth() As Integer
            Get
                Return _MenuWidth
            End Get
            Set(ByVal value As Integer)
                _MenuWidth = value
            End Set
        End Property
        Property MenuHeight() As Integer
            Get
                Return _MenuHeight
            End Get
            Set(ByVal value As Integer)
                _MenuHeight = value
            End Set
        End Property
        Property MenuOverflow() As String
            Get
                Return _MenuOverflow
            End Get
            Set(ByVal value As String)
                _MenuOverflow = value
            End Set
        End Property
        Property AnimationSteps() As Integer
            Get
                Return _AnimationSteps
            End Get
            Set(ByVal value As Integer)
                _AnimationSteps = value
            End Set
        End Property
        Property AnimationDelay() As Integer
            Get
                Return _AnimationDelay
            End Get
            Set(ByVal value As Integer)
                _AnimationDelay = value
            End Set
        End Property
        Property OffsetLeft() As Integer
            Get
                Return _OffsetLeft
            End Get
            Set(ByVal value As Integer)
                _OffsetLeft = value
            End Set
        End Property
        Property OffsetTop() As Integer
            Get
                Return _OffsetTop
            End Get
            Set(ByVal value As Integer)
                _OffsetTop = value
            End Set
        End Property
        Property ExpandDirection() As ExpandDirections
            Get
                Return _ExpandDirection
            End Get
            Set(ByVal value As ExpandDirections)
                _ExpandDirection = value
            End Set
        End Property
#End Region
        Protected Overrides Sub CreateChildControls()
            'If Not Menu Is Nothing Then
            '    Controls.Clear()
            '    Controls.Add(Menu)
            'End If
        End Sub
        Protected Overrides Sub Render(ByVal output As HtmlTextWriter)
            output.AddAttribute("class", CssClass)
            output.AddAttribute("onclick", "window." & ClientID & ".Toggle()")
            output.AddAttribute("id", ClientID)
            output.RenderBeginTag(HtmlTextWriterTag.Div)
            output.Write(Text)
            output.RenderEndTag()

            output.AddAttribute("class", MenuCss)
            output.AddStyleAttribute("position", "absolute")
            output.AddStyleAttribute("display", "none")
            output.AddStyleAttribute("overflow", MenuOverflow)
            output.AddAttribute("id", ClientID & "_div")
            output.RenderBeginTag(HtmlTextWriterTag.Div)
            If Not Menu Is Nothing Then
                Menu.RenderControl(output)
            End If
            output.RenderEndTag()

            Dim script As String = "<script type=""text/javascript"">window." & ClientID & "=new ActiveMenuButton('" & ClientID & "'," & MenuWidth & "," & MenuHeight & "," & AnimationSteps & "," & AnimationDelay & "," & OffsetTop & "," & OffsetLeft & "," & ExpandDirection & ");</script>"
            output.Write(script)
        End Sub

        Private Sub MenuButton_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
            If Not Menu Is Nothing Then
                Controls.Clear()
                Controls.Add(Menu)
            End If
        End Sub

        Private Sub MenuButton_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Not Page.ClientScript.IsClientScriptIncludeRegistered("AMMenu") Then
                Page.ClientScript.RegisterClientScriptInclude("AMMenu", Page.ClientScript.GetWebResourceUrl(Me.GetType, "MenuButton.js"))
            End If
        End Sub
    End Class
    Public Class MenuContent
        Inherits Control
    End Class
End Namespace